//
//  YJLoginViewModel.m
//  YJB
//
//  Created by YJB on 2016/11/17.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "YJLoginViewModel.h"
#import "YJNetworkTool.h"
#import "YJUserDefaultTool.h"
#import "YJNetworkTool.h"
//#import "YJTabBarC.h"
#import "THHomeVC.h"
#import "YJBaseVC.h"
#import "THTabBarC.h"
#import "YJBaseNav.h"

@implementation YJLoginViewModel
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (void)setup
{
    _loginBtnEnableSignal = [RACSignal combineLatest:@[RACObserve(self, username),RACObserve(self, pwd)] reduce:^id(NSString *username,NSString *pwd){
        return @(username.length && pwd.length);
    }];;
    
    @weakify(self);
    _loginCommand  = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            @strongify(self);
            //做网络请求
            [self sendRequestWith:subscriber];
            return nil;
        }];
    }];
}

- (void)sendRequestWith:(id<RACSubscriber>)subscriber
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //去除账号首尾用户名
    NSString *noSpaceAcount =  [_username stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *md5key = [[[noSpaceAcount stringByAppendingString:@"iOS"]stringByAppendingString:appMd5Key]md5];
    param[@"action"] = @"login";
    param[@"makegold"] = @{@"username":noSpaceAcount,
                           @"password":[_pwd md5],
                           @"user_info":noSpaceAcount,
                           @"phone_info":@"iOS",
                           @"md5_key":md5key
                           };
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [MBProgressHUD showMessage:@"" toView:keyWindow];
    [YJNetworkTool postWithUrl:NEWURL(@"user.php") parameters:param success:^(id responseObject)
     {
         [MBProgressHUD hideHUDForView:keyWindow];
         NSString *code = responseObject[@"return"][@"code"];
         if (code.integerValue == 200)
         {
             NSString *userId = responseObject[@"return"][@"data"][@"user_id"];
             NSString *iconUrl = responseObject[@"return"][@"data"][@"image"];
             NSString *carId = responseObject[@"return"][@"data"][@"card_id"];
             NSString *carNum = responseObject[@"return"][@"data"][@"card_no"];
             NSString *userName = responseObject[@"return"][@"data"][@"user_name"];
              NSString *keyID = [[userName stringByAppendingString:@"iOS"]stringByAppendingString:appMd5Key];
             NSString *keyIDMd5 = [keyID md5];
             
             //缓存账号
             [YJUserDefaultTool saveUserAccount:noSpaceAcount];
             //缓存用户名
             [YJUserDefaultTool saveUserName:userName];
             [YJKeychainTool    savePassword:_pwd];
             [YJUserDefaultTool saveUserID:userId];
             [YJUserDefaultTool saveUserIconUrl:iconUrl];
             [YJUserDefaultTool saveLoginStatus:YES];
             [YJUserDefaultTool saveKeyID:keyIDMd5];
             [YJUserDefaultTool saveCardId:carId];
             [YJUserDefaultTool saveCardNumber:carNum];
             
            // [UIApplication sharedApplication].keyWindow.rootViewController = [YJTabBarC new];
             
             THTabBarC *vc = [[THTabBarC alloc] init];
             YJBaseNav *nav = [[YJBaseNav alloc] initWithRootViewController:vc];
             [UIApplication sharedApplication].keyWindow.rootViewController = nav;
             //rac发送数据
             [subscriber sendNext:responseObject];
         }
         else
         {
             [MBProgressHUD showError:responseObject[@"return"][@"message"]];
         }
         [subscriber sendCompleted];
     } failure:^(NSError *error) {
         [MBProgressHUD hideHUDForView:keyWindow];
         [MBProgressHUD showError:@"网络不佳,稍后重试"];
         [subscriber sendCompleted];
     }];
}


@end
